<?php

namespace app\api\service;
use app\admin\model\CouponRecord;
use app\admin\model\User;
use app\common\model\Advance;
use app\common\model\Goodorder;
use app\common\model\member\Order;
use app\common\model\Occupants;
use app\common\model\Payment;
use app\common\model\Reservations;
use app\common\model\Rooms;
use think\Db;

error_reporting(E_ERROR);
class MemorderService
{


    /**
     * 结算
     * @return mixed
     */
    public static function order_Payment($id,$uid)
    {
        $order=Order::where('id','=', $id)->find();
        if (!$order) {
            fail('订单不存在');
        }
        if ($order['pay_status'] != 0) {
            fail('订单状态错误');
        }
        $user_info = User::where('id','=',$uid)->find();
        if (!$user_info || !$user_info['openid']) {
            fail('用户信息不存在，或未绑定微信，请重新登录后再试');
        }
        return \addons\epay\library\Service::submitOrder($order['paid'], $order['order_no'], "wechat",'会员商城', request()->domain() . '/api/notify/mem_order_pay', "", "miniapp",$user_info['openid']);
    }

    /*
     * 支付成功后
     */
    public function order_success($id)
    {

        $payment = Order::where('id', $id)->find();
        if ($payment['pay_status'] == 1) {
            fail('该订单已支付');
        }
        $result = Order::where('id', $id)->where('pay_status', 0)->update(['pay_status' => 1,'pay_time'=>time(),'status'=>1]);
        if ($result == 0) {
            fail('该订单已支付');
        }
        $user=User::where('id',$payment['user_id'])->find();

        if($payment['balance']>0) {
            \app\common\model\User::money(-$payment['balance'],$payment['user_id'],'消费-会员商城');
        }

    }

}